package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author Pan Rundong
 * @Date 2023-03-04 16:32
 * @Desc 通信Controller
 */
@RestController
@RequestMapping("messages")
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    /**
     * 根据环信Id查询User信息
     *
     * @param huanxinId
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.UserInfoVo>
     * @Author Pan Rundong
     */
    @GetMapping("userinfo")
    public ResponseEntity<UserInfoVo> userInfo(String huanxinId) {
        //1.调用Service方法
        UserInfoVo userInfoVo = messagesService.userInfo(huanxinId);
        //2.返回
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 添加联系人
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     * @Author Pan Rundong
     */
    @PostMapping("contacts")
    public ResponseEntity contacts(Map map) {
        Long friendId = Long.valueOf(map.get("userId").toString());
        messagesService.contacts(friendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 联系人列表
     *
     * @param page
     * @param pagesize
     * @param keyword
     * @return org.springframework.http.ResponseEntity<com.tanhua.model.vo.ContactVo>
     * @Author Pan Rundong
     */
    @GetMapping("contacts")
    public ResponseEntity<PageResult> getContacts(@RequestParam(defaultValue = "1") Integer page,
                                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                                  String keyword)
    {
        PageResult pageResult = messagesService.getContacts(page, pagesize, keyword);
        return ResponseEntity.ok(pageResult);
    }
}
